////////SOU ///////////

package com.example.testerclientapp;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
//import android.text.format.DateFormat;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import java.lang.Integer;


//import java.sql.Date;
//import java.text.DateFormat;
//import java.text.SimpleDateFormat;
//import java.util.Date;
//import java.text.DateFormat;


public class ReservationSpotActivity extends Activity implements OnItemSelectedListener{

	EditText email,garageid;
	Button reserve;
	String item;
	private static final String SOAP_ACTION = "http://spartansparking.org/getAvailableSpotByUserType"; 
    private static final String METHOD_NAME =  "getAvailableSpotByUserType";       
    private static final String NAMESPACE =    "http://spartansparking.org/";    
    private static final String URL = "http://spartanparking.azurewebsites.net/parkingservice.asmx";   
    private AndroidHttpTransport mAht;
    public  SoapSerializationEnvelope mSoapEnvelope;
    // Apply the adapter to the spinner
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reservation_spot);
        
        ///////////////////////////////////////////starts here//////////////////////////
        
        reserve=(Button)findViewById(R.id.sign_u);
        
        final Spinner spinner = (Spinner) findViewById(R.id.spinner1);
        final Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
        final Spinner spinner3 = (Spinner) findViewById(R.id.spinner3);
        final Spinner spinner4 = (Spinner) findViewById(R.id.spinner4);
        
        
     ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
             R.array.planets_array, android.R.layout.simple_spinner_item);
     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
     spinner.setAdapter(adapter);
     
     
     ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(this,
             R.array.planets_array, android.R.layout.simple_spinner_item);
     adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
     spinner2.setAdapter(adapter1);
     
     ArrayAdapter<CharSequence> adapter3 = ArrayAdapter.createFromResource(this,
             R.array.garages, android.R.layout.simple_spinner_item);
     adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
     spinner3.setAdapter(adapter3);
     
     ArrayAdapter<CharSequence> adapter4 = ArrayAdapter.createFromResource(this,
             R.array.user, android.R.layout.simple_spinner_item);
     adapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
     spinner4.setAdapter(adapter4);
        
        
        ////////////////////////////////////////////////////////////////////////////
        
        reserve.setOnClickListener(new View.OnClickListener()
        {
        	@Override
        	public void onClick(View v)
        	{
        		Intent getIntentVal = getIntent();
                String email=getIntentVal.getStringExtra("email");
                //Toast.makeText(getApplicationContext(), "My email is"+email, Toast.LENGTH_LONG).show();
                if((email==null))
                {
                	email="dummy";
                }
        //email=(EditText)findViewById(R.id.EditText01);
                
                
                
                
                
                
                
        //garageid = (EditText)findViewById(R.id.gr);
        
        String starttime = spinner.getSelectedItem().toString();
        String endtime = spinner2.getSelectedItem().toString();
        String garage = spinner3.getSelectedItem().toString();
        String usertype = spinner4.getSelectedItem().toString();
        
        //DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
        //Date date = new Date(0);
        
        //Toast.makeText(getApplicationContext(), date, Toast.LENGTH_LONG);
             
        //int startofthe=Integer.valueOf(starttime);
        //int end1 = Integer.valueOf(endtime);
        
        //int start1 = spinner.getSelectedItemPosition(); 
        //int end1= spinner2.getSelectedItemPosition();  
        
     //Toast.makeText(getApplicationContext(), starttime, Toast.LENGTH_SHORT).show();
     //Toast.makeText(getApplicationContext(), endtime, Toast.LENGTH_SHORT).show();
     //email = (EditText)findViewById(R.id.EditText01);
     //garageid = (EditText)findViewById(R.id.gr);
     //EditText usertype = (EditText)findViewById(R.id.u_type);
     //int grdgid;
     //if(end1>startofthe)
    	 
     {
     
     ///////////////////////////// SOU ///////////////////////////
     /////////////////CONSTRUCT ENVELOPE AND POST
    	 
    	 try
	        {
    		 int grgid; int user;
    		 // SOU /////////// String garage = spinner3.getSelectedItem().toString();
    	        
    	        //Toast.makeText(getApplicationContext(), garage, Toast.LENGTH_LONG).show();
    	        
    	        if(garage.equalsIgnoreCase("South Garage"))
    	        	
    	        {
    	        	grgid=1;
    	        	
    	        }
    	        
    	        else if(garage.equalsIgnoreCase("West Garage"))
    	        	
    	        {
    	        	grgid=2;
    	        }
    	        else 
    	        {
    	            grgid = 3;
    	        }
    	        
if(usertype.equalsIgnoreCase("Student"))
    	        	
    	        {
    	        	user=1;
    	        	
    	        }
    	        
    	        else if(usertype.equalsIgnoreCase("Staff"))
    	        	
    	        {
    	        	user=2;
    	        }
    	        else 
    	        {
    	            user = 3;
    	        }

               //Toast.makeText(getApplicationContext(), Integer.toString(user), Toast.LENGTH_LONG).show();
               
		        //int grgid=Integer.valueOf(garageid.getText().toString());
		        //int user=Integer.valueOf(usertype.getText().toString());
		        
		        //String string1 = "05/05/2013";
		        //String string2 = "05/05/2013";
		        starttime = starttime;
		        endtime = endtime;
		        
		        
		        //if ((garageid.getText().toString().length() != 0) && (usertype.getText().toString().length()!=0))
		        if ((user!=0))
		        {
		        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		        //request.addProperty("email", email.getText().toString());
		        request.addProperty("garageId", grgid);
		        request.addProperty("starttime",starttime);
		        request.addProperty("endtime",endtime);
		        request.addProperty("UserType",user);
		        
		        
		        //Toast.makeText(getApplicationContext(), garageid.toString(), Toast.LENGTH_LONG);
		        //Toast.makeText(getApplicationContext(), starttime, Toast.LENGTH_LONG);
		        //Toast.makeText(getApplicationContext(), endtime, Toast.LENGTH_LONG);
		        //Toast.makeText(getApplicationContext(), usertype.toString(), Toast.LENGTH_LONG);
		        
		        
		        
		        mSoapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		        mSoapEnvelope.dotNet = true;
		        mSoapEnvelope.setOutputSoapObject(request);
		        
		        mAht = new AndroidHttpTransport(URL);
	        	mAht.call(SOAP_ACTION, mSoapEnvelope);   //in-out parameter: soapEnvelope, which means the response back will be stored in this var soapEnvelope
	        	//Toast.makeText(getApplicationContext(), mSoapEnvelope.toString(), Toast.LENGTH_LONG);
	        	
	        	 if (mSoapEnvelope.bodyIn instanceof SoapFault)
	        	 {
	        		 String str= ((SoapFault) mSoapEnvelope.bodyIn).faultstring;
	        		 
	        	}
	        	else 
	        		 {
	              		 SoapPrimitive resultsRequestSOAP = (SoapPrimitive)mSoapEnvelope.getResponse();
		     
		        		 //if (resultsRequestSOAP.toString().contains("available"))
		        		 {
	//Toast.makeText(getApplicationContext(), resultsRequestSOAP.toString(), Toast.LENGTH_LONG);
		        			 //createAndShowDialog(resultsRequestSOAP.toString(), "Server Response");
	             //Toast.makeText(getApplicationContext(), resultsRequestSOAP.toString(), Toast.LENGTH_LONG).show();
		        			 Intent reservationSpotActivity = new Intent(ReservationSpotActivity.this, ShowAvailableSpot.class);
		        			 reservationSpotActivity.putExtra("AvailableSpots", resultsRequestSOAP.toString());
		        			 reservationSpotActivity.putExtra("username", email.toString());
		        			 reservationSpotActivity.putExtra("Emai", email.toString());
		        			 reservationSpotActivity.putExtra("starttime", starttime);
		        			 reservationSpotActivity.putExtra("endtime", endtime);
		        			 startActivity(reservationSpotActivity);
		        			 
		        			 
		        		 }
		        		 //else
		        		 	{
		        			 	// return "false" means no user found in database
		        			 	//createAndShowDialog(resultsRequestSOAP.toString(), "Server Response");
		        		 	}
	        		 }
	       
	        }
	        }
	       
	        catch (Exception ex)
	        {
	        	ex.printStackTrace();
	           	Toast.makeText(getBaseContext(), "Error: "+ex.getMessage(), Toast.LENGTH_LONG).show();
	        }
   
     
     
     //////////////////////////////////////////////////////////

        
        
     }
     
     //else 
     //{
    	 //Toast.makeText(getApplicationContext(), "Start Time should be less than end time", Toast.LENGTH_SHORT).show();
     //}
        	}
        	});
        
        
    }

    
    	
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
        
        item = (String) parent.getItemAtPosition(pos).toString();
        Toast.makeText(parent.getContext(), 
        		"OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
        		Toast.LENGTH_SHORT).show();
        
    }
    
    public void onNothingSelected(AdapterView<?> parent) {
        // Another interface callback
    }

    
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_reservation_spot, menu);
        return true;
    }
    
    
    public void createAndShowDialog(String message, String title) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);

		builder.setMessage(message);
		builder.setTitle(title);
		builder.create().show();
	}
     
    
    
    
}





